package com.platform.modules.wallet.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.platform.common.exception.BaseException;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class VerifyIdcardUtil extends AppCode {

    public static void main(String[] args) {
//        verifyPhoto("http://qiniu.weipinji.vip/%E6%9D%A8%20%E6%AD%A31.jpg", "http://qiniu.weipinji.vip/%E6%9D%A8%20%E5%8F%8D1.jpg");
    }

    public static boolean verify(String realName, String idcard, String frontUrl, String negativeUrl) {
        String host = "https://idocrhj.market.alicloudapi.com";
        String path = "/ocr/idcard";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
//        bodys.put("front", "front");
        bodys.put("frontUrl", frontUrl);
//        bodys.put("negative", "negative");
        bodys.put("negativeUrl", negativeUrl);

        String result = null;

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//            System.out.println(response.toString());
            //获取response的body
            result = EntityUtils.toString(response.getEntity());
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (jsonObject.getInt("code") != 200) {
            throw new BaseException(jsonObject.getStr("msg"));
        }

        JSONObject data = jsonObject.getJSONObject("data");
        if (!data.getStr("name").equals(realName)) {
            throw new BaseException("姓名不匹配");
        }
        if (!data.getStr("idcard").equals(idcard)) {
            throw new BaseException("身份证号不匹配");
        }


        return true;
    }

    public static boolean verify(String realName, String idcard) {

        String host = "https://zid.market.alicloudapi.com";
        String path = "/idcard/VerifyIdcardv2";
        String method = "GET";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("cardNo", idcard);
        querys.put("realName", realName);


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            String str = EntityUtils.toString(response.getEntity());
            System.out.println("身份验证信息返回：" + str);
            JSONObject jsonObject = JSONUtil.parseObj(str);

            if (jsonObject.get("error_code").equals(0)) {
                JSONObject result = jsonObject.getJSONObject("result");
                return result.getBool("isok");
            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public static boolean verify(String name, String number, String url) {
        String host = "https://slyidcar.market.alicloudapi.com";
        String path = "/idcard_ocr";
        String method = "POST";
//        String appcode = "你自己的AppCode";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
//        bodys.put("image", "image");
        bodys.put("url", url);

        String result = null;
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));

            result = EntityUtils.toString(response.getEntity());
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (jsonObject.getInt("code") != 200) {
            throw new BaseException(jsonObject.getStr("msg"));
        }

        JSONObject info = jsonObject.getJSONObject("data").getJSONObject("info");
        if (!info.getStr("name").equals(name)) {
            throw new BaseException("姓名不匹配");
        }
        if (!info.getStr("number").equals(number)) {
            throw new BaseException("身份证号不匹配");
        }

        return true;
    }
}
